package com.hjb.annotation;

import com.hjb.config.DatasourceRouteKeyHolder;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @author HuJiaBing
 * @date 2025/7/7 21:56
 */
@Aspect
@Component
@Slf4j
@Order(1)
public class UsingDbAspect {
    @Before("@annotation(usingDb)")
    void before(UsingDb usingDb) {
        // 切换数据源
        String dbName = usingDb.value();
        DatasourceRouteKeyHolder.setDataSourceRouteKey(dbName);
        log.info("切换数据源为:{}", dbName);
    }

    @After("@annotation(UsingDb)")
    void after () {
        DatasourceRouteKeyHolder.clearDataSourceKey();
        log.error("清除key");
    }

}
